package com.msmall.community.common.action.wml;

import com.msmall.base.action.WMLBaseAction;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;

public class RegisterAction extends WMLBaseAction {
	
	private static final long serialVersionUID = 3613546635443277076L;

	private String account;
	private String password;
	private String nick;
	private String email;
	
	private UserManager userManager;
	
	public String gr(){
		return SUCCESS;
	}
	public String register() {
		if(!validRegister()){
			return ERROR;
		}
		try {
			UserInfo userInfo = new UserInfo();
			userInfo.setUsername(account);
			userInfo.setNickname(nick);
			userInfo.setPassword(password);
			userInfo.setEmail(email);
			userManager.register(userInfo);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	
	private boolean validRegister(){
		boolean tag = true;
		if(userManager.isUsernameExisted(account)){
			addFieldError("account", getText("sys.register.username.existed"));
			tag =  false;
		}
		return tag;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}
